package com.liutao.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.liutao.entity.Talents;
import com.liutao.entity.Title;
import com.liutao.enums.TalentsEnum;
import com.liutao.service.TalentsService;

@Controller
public class TalentsController {
	@Autowired
	private TalentsService service;
	@RequestMapping("list")
	public String list(@RequestParam(defaultValue = "1")Integer pageNum,
			Talents talents,Model model) {
		//用于模糊查询职称
		List<Title> title = service.findTitleNames();
		//列表
		PageInfo<Talents> info = service.list(pageNum,talents);
		
		model.addAttribute("title", title);
		model.addAttribute("talents", talents);
		model.addAttribute("info", info);
		return "list";
	}
	//跳转添加页面
	@RequestMapping("toAdd")
	public String toAdd(Model model) {
		//查询所有职称
		List<Title> title = service.findTitleNames();
		//枚举性别
		TalentsEnum[] talentsEnums = TalentsEnum.values();
		
		model.addAttribute("talents", new Talents());
		model.addAttribute("title", title);
		model.addAttribute("talentsEnums", talentsEnums);
		return "add";
	}
	//添加
	@RequestMapping("add")
	public String add(@Valid @ModelAttribute("talents")Talents talents , BindingResult br,Model model) {
		if(br.hasErrors()) {
			//查询所有职称
			List<Title> title = service.findTitleNames();
			//枚举性别
			TalentsEnum[] talentsEnums = TalentsEnum.values();
			model.addAttribute("title", title);
			model.addAttribute("talentsEnums", talentsEnums);
			return "add";
		}
		service.add(talents);
		return "redirect:list";
	}
	@RequestMapping("toUpdate")
	public String toUpdate(Integer id,Model model) {
		//根据id查一个对象
		Talents talent = service.findById(id);
		//查询已经选的职称
		Integer[] tids = service.findCenterById(id);
		talent.setTids(tids);
		
		//查询所有职称
		List<Title> title = service.findTitleNames();
		//枚举性别
		TalentsEnum[] talentsEnums = TalentsEnum.values();
		
		model.addAttribute("talents", talent);
		model.addAttribute("title", title);
		model.addAttribute("talentsEnums", talentsEnums);
		model.addAttribute("id", id);
		
		return "update";
	}
	//修改
	@RequestMapping("update")
	public String update(@Valid @ModelAttribute("talents")Talents talents , BindingResult br,Model model) {
		if(br.hasErrors()) {
			//查询所有职称
			List<Title> title = service.findTitleNames();
			//枚举性别
			TalentsEnum[] talentsEnums = TalentsEnum.values();
			model.addAttribute("title", title);
			model.addAttribute("talentsEnums", talentsEnums);
			return "update";
		}
		service.update(talents);
		return "redirect:list";
	}
	//删除
	@RequestMapping("del")
	@ResponseBody
	public Boolean del(Integer id) {
		if(id !=0) {
			service.del(id);
			return true;
		}else {
			return false;
		}
	}
}
